/**
 * 
 */
package neptune.web.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.DisclosurePanelImages;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class realizes an ApplicationPanel with a MenuBar, a Console to show
 * messages, and a DeskPanel to select a particular view.
 * 
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class OperativeView extends SuperView implements HasConsole {

	public static final String DEFAULT_TOKEN = "Operative";
	private FlowPanel console;
	private ScrollPanel consoleWrap;
	private MenuBar topMenuBar;
	private Label userNameLabel;
	private HorizontalPanel topMenuContainer;

	public OperativeView() {
		this(buildTopMenuBar());
	}

	/**
	 * Builds the interface
	 */
	public OperativeView(MenuBar menuBar) {
		// Setting view params
		try {
			this.setName(DEFAULT_TOKEN);
		} catch (DuplicatedTokenException e) {
			ConsoleManager.addConsoleDebugMessage(e.getMessage(), true);
		}

		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.setSize("100%", "100%");
		userNameLabel = new Label("username - User Role");
		userNameLabel.addStyleName("OperativeView-username");
		// TopMenuBar building
		topMenuBar = menuBar;

		topMenuContainer = new HorizontalPanel();
		topMenuContainer.addStyleName("OperativeView-topMenuBarContainer");
		topMenuContainer.add(topMenuBar);
		topMenuContainer.add(userNameLabel);

		// Console building
		DisclosurePanel consolePanel = buildConsole();
		// DesktopPanel building
		DeckPanel desktopPanel = new DeckPanel();
		this.setViewContainer(desktopPanel);
		desktopPanel.setSize("100%", "100%");

		mainPanel.add(topMenuContainer);
		mainPanel.add(consolePanel);
		mainPanel.add(desktopPanel);

		mainPanel.addStyleName("OperativeView");
		initWidget(mainPanel);
	}

	private DisclosurePanel buildConsole() {
		// ImageBundle creation, to give images to the disclosure console
		DisclosurePanelImages disclusureImages = (DisclosurePanelImages) GWT
				.create(DisclosurePanelImages.class);

		DisclosurePanel consolePanel = new DisclosurePanel(disclusureImages,
				"Click here to open/close console", false);

		console = new FlowPanel();
		consoleWrap = new ScrollPanel(console);
		consoleWrap.addStyleName("OperativeView-consoleWrap");

		consolePanel.setContent(consoleWrap);

		Widget header = consolePanel.getHeader();
		header.addStyleName("OperativeView-consoleHeader");

		return consolePanel;
	}

	private static MenuBar buildTopMenuBar() {
		MenuBar topMenuBar = new MenuBar();
		return topMenuBar;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * neptune.web.client.view.HasConsole#addConsoleMessage(java.lang.String,
	 * boolean)
	 */
	public void addConsoleMessage(String message, boolean isError) {

		if (console.getWidgetCount() > 100)
			console.getWidget(0).removeFromParent();

		Label messageLabel = new Label(message);
		if (!isError) {
			messageLabel.addStyleName("OperativeView-consoleText-norm");
		} else
			messageLabel.addStyleName("OperativeView-consoleText-err");

		console.add(messageLabel);
		consoleWrap.setScrollPosition(console.getOffsetHeight());
	}

	/**
	 * Returns the actual MenuBar
	 * 
	 * @return
	 */
	public MenuBar getTopMenuBar() {
		return topMenuBar;
	}

	/**
	 * Sets the interface MenuBar
	 * 
	 * @param topMenuBar
	 */
	public void setTopMenuBar(MenuBar topMenuBar) {
		this.topMenuBar = topMenuBar;
		Widget name = this.topMenuContainer.getWidget(1);
		this.topMenuContainer.remove(1);
		this.topMenuContainer.remove(0);
		this.topMenuContainer.add(topMenuBar);
		this.topMenuContainer.add(name);
	}

	/**
	 * Returns the actual username label
	 * 
	 * @return
	 */
	public String getUsernameLabel() {
		return userNameLabel.getText();
	}

	/**
	 * Sets the interface top-right label
	 * 
	 * @param usernameLabel
	 */
	public void setUsernameLabel(String usernameLabel) {
		this.userNameLabel.setText(usernameLabel);
	}

	/**
	 * Sets the interface top-right label
	 * 
	 * @param username
	 * @param role
	 */
	public void setUsernameLabel(String username, String role) {
		setUsernameLabel(username + " - " + role);
	}
}
